package com.ricchieri.manage.bean;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.filter.CharterFilter;
import com.ricchieri.util.FacesUtil;

@ManagedBean(name = "addCharterExpenseMB")
@ViewScoped
public class AddCharterExpenseManageBean extends CommonCharterExpenseManageBean {

	private static final long serialVersionUID = -2708013969378031014L;

	private CharterFilter charterFilter;
	
	@PostConstruct
	public void init() {
		super.init();
		charterFilter = (CharterFilter) FacesUtil.getParametersFlash(BusinessSettings.KEY_FILTER);
	}
	
	public String save() {
		getCharterExpense().setCharterExpenseType(obtainCharterExpenseType(getSelectedCharterExpenseType()));
		getCharterExpense().setCharter(getCharter());
		getCharterExpenseService().addCharterExpense(getCharterExpense());
		FacesUtil.addParametersFlash(BusinessSettings.KEY_CHARTER, getCharter());
		FacesUtil.addParametersFlash(BusinessSettings.KEY_CHARTER_EXPENSE, getCharterExpense());
		return BusinessSettings.SUCCESS;
	}

	public String doBackToSearchCharter() {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, charterFilter);
		
		return BusinessSettings.SEARCH_CHARTER;
	}
	
	public String doBackToViewCharter() {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, charterFilter);
		FacesUtil.addParametersFlash(BusinessSettings.KEY_CHARTER, getCharter());
		
		return BusinessSettings.VIEW_DETAILS;
	}
}
